package me.luger.project.fof.controller.admin;

import me.luger.core.dao.nutz.entity.page.DataTableRequest;
import me.luger.core.mvc.controller.BaseController;
import me.luger.core.mvc.entity.Result;
import me.luger.core.mvc.interceptor.oplog.OpLog;
import me.luger.project.fof.service.CompanyService;
import me.luger.project.fof.service.UserInfoService;
import me.luger.project.fof.entity.Company;
import me.luger.project.fof.entity.UserInfo;
import org.nutz.dao.Sqls;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * UserInfo controller
 */
@Controller
@RequestMapping("/private/fof/userInfo")
public class UserInfoController extends BaseController {

    private String viewBasePath = "/admin/modules/userInfo/";

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private CompanyService companyService;

    @RequestMapping(value = "",name = "UserInfo列表页面")
    public ModelAndView index(){
        return btlView(viewBasePath + "index");
    }

    @RequestMapping(value = "data",name = "获取UserInfo列表")
    @ResponseBody
    public Object data(@RequestBody final DataTableRequest dataTableRequest){
        return userInfoService.data(dataTableRequest, Sqls.create("select u.*,c.`name` as companyName from fof_user_info u left join fof_company c on u.companyId = c.id $condition"));
    }



    @RequestMapping(value = "delete",name = "删除UserInfo接口")
    @ResponseBody
    public Object delete(String[] ids){
        userInfoService.delete(ids);
        return Result.success("delete success");
    }

    @RequestMapping(value = "/detail/{id}",name = "获取UserInfo详细信息接口")
    public ModelAndView detail(@PathVariable("id") Integer id){
        UserInfo userInfo = userInfoService.fetchById(id);
        setAttr("userInfo",userInfo);
        return btlView(viewBasePath + "detail");
    }

    @RequestMapping(value = "add",name = "添加UserInfo页面")
    public ModelAndView add(){
        List<Company> companyList = companyService.list();
        setAttr("companyList",companyList);
        return btlView(viewBasePath + "add");
    }

    @RequestMapping(value = "addDo",name = "添加UserInfo接口")
    @ResponseBody
    @OpLog(name = "添加UserInfo",msg = "添加UserInfo}")
    public Object adddo(UserInfo userInfo){
        try{
            userInfo.setRoleId(0);
            userInfo = userInfoService.insert(userInfo);
        }catch (Exception e){
            return Result.error();
        }
        return Result.success();
    }

    @RequestMapping(value = "/edit/{id}",name = "更新UserInfo信息页面")
    public ModelAndView edit(@PathVariable Integer id){
        UserInfo userInfo = userInfoService.fetchById(id);
        setAttr("userInfo",userInfo);
        List<Company> companyList = companyService.list();
        setAttr("companyList",companyList);
        return btlView(viewBasePath + "edit");
    }

    @RequestMapping(value = "editDo",name = "更新UserInfo接口")
    @ResponseBody
    public Object editDo(UserInfo userInfo){
        try{
            userInfoService.updateIgnoreNull(userInfo);
        }catch (Exception e){
            return Result.error();
        }
        return Result.success();
    }
}
